package com.xiaofeng.pochi.advice;

import com.xiaofeng.pochi.common.Result;
import com.xiaofeng.pochi.enums.ResultEnums;
import com.xiaofeng.pochi.exception.PochiException;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/07/07/23:40
 * @Description:
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionAdvice {
    /**
     * 全局处理自定义异常
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(PochiException.class)
    public Result<?> exceptionHandler(PochiException exception) {
        log.error("统一异常处理：", exception);
        return new Result<>(exception.getErrCode(), exception.getMessage());
    }

    /**
     * 处理权限不足异常
     * @param exception
     * @return
     */
    @ExceptionHandler(AuthorizationException.class)
    public Result<?> authorizationHandler(AuthorizationException exception) {
        log.error("权限不足异常处理，", exception);
        return new Result<>(ResultEnums.AUTH_NOT_FOUNT);
    }
}
